Skip to content

fix(serve): refuse unhashable tournament choice labels with a 422, not a 500 - #1366

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-serve-tournament-422
Oct 25, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-serve-tournament-422

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

With LAYA_TOURNAMENT_GROUP >= 2 (documented, docs/http-api.md), a large choice is narrowed by elimination. laya/serve.py::_tournament_plan reads every oversized choice's criteria labels — and _criteria_items de-duplicates them through a set — before router.predict runs. A criteria written as a list of JSON arrays/objects gives an unhashable label:

_tournament_plan({"queue": {"type": "choice", "criteria": [["a", 1], ["b", 2], ["c", 3]]}}, 2)
  -> TypeError: cannot use 'list' as a set element (unhashable type: 'list')   # shortlist.py:464

The tournament gate at serve.py:432 runs before router.predict, so core's Agent._check_question never sees it; the TypeError fell through the endpoint's catch-all and answered 500 "inference failed" with a traceback per request.

Why

The endpoint documents a 422 that names the offending question, and every sibling refusal already produces exactly that. The same create_app() with LAYA_TOURNAMENT_GROUP unset answers the identical request with a clean 422 naming queue (core's _check_question); and inside _tournament_plan the duplicate-label and empty-list ValueErrors from _criteria_items already propagate to a 422. Only the unhashable-label case, which surfaces as a TypeError, reaches the 500 — the asymmetry is the bug. Fixing it in _tournament_plan is the one site that touches a request body before core validation, so no core behaviour changes.

How it was verified

# group OFF -> 422 (already fine); group=2 bad labels -> was 500, now 422; group=2 valid -> 200 unchanged:
python -c "from laya import serve; serve._tournament_plan({'queue':{'type':'choice','criteria':[['a',1],['b',2],['c',3]]}}, 2)"
# -> ValueError: question 'queue': cannot use 'list' as a set element ...   (was TypeError)

python -m pytest tests/test_serve.py -k tournament -q          # 20 passed
python -m pytest tests/test_serve.py -q                         # 316 -> 318 passed
python -m compileall -q laya/serve.py tests/test_serve.py
ruff check laya/serve.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
# mutation arm: reverting the guard re-leaks the TypeError from shortlist.py:464 and both new
#   tests fail by name (500 instead of 422); restoring -> 318 passed.

Regression cases go into the already-registered tests/test_serve.py, in the LAYA_TOURNAMENT_GROUP section next to test_large_choice_is_refused_without_a_tournament.

Checklist

  • Focused on one change
  • Rebased on latest main
  • Tests pass locally
  • Docs updated when public API changed (no API change; this makes the response match the 422 documented in docs/http-api.md, which previously only held on the non-tournament path)

…t a 500

When LAYA_TOURNAMENT_GROUP is on, _tournament_plan reads a large choice's
criteria labels -- de-duplicating them through a set -- BEFORE router.predict
runs, so core's Agent._check_question (which refuses a non-scalar label with a
ValueError naming the question) has not seen the request yet. A criteria written
as a list of JSON arrays/objects therefore raised a bare
`TypeError: cannot use 'list' as a set element`, which fell through the endpoint's
catch-all and answered 500 "inference failed" with a traceback per request.

Wrap the label read and re-raise the TypeError as the same ValueError the
sibling duplicate-/empty-label paths already raise, so an unhashable label is a
422 naming the question -- matching the no-tournament behaviour, where the same
input already returns a clean 422.

Tests: tests/test_serve.py tournament section, +2 (316 -> 318).
@NandhaKishorM
NandhaKishorM merged commit c1390ab into NandhaKishorM:main Oct 25, 2026
51 of 52 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 25, 2026
8 PRs. Rust and .NET batch prediction and router batch APIs (#1358,
#1359), the min_confidence abstention gate and jev_confidence in both
ports (#1362, #1363), and four inputs refused where they enter instead
of failing later (#1365, #1366, #1367, #1368).

Verified before tagging: all 92 suites pass, 612 tests. ruff clean on
both lists. Java fixture, Unicode table and language table gates clean;
three Go tables_gen.go match. Version agrees across pyproject.toml,
laya.__version__ and nix/package.nix. README carries 0.4.16 only.

Security audit of the merge diff, 180330 added lines over 349 files:
zero dynamic exec, zero network calls, zero URLs, zero base64 or
obfuscation, zero secrets in the diff or the tracked tree, zero
laya-max boundary hits, no writes outside the test temp dir, the
dependency surface untouched, no npm install hooks, no binaries, every
new file 100644, and 20 of 20 actions SHA-pinned with codeql-action
init, autobuild and analyze all on 4.39.1.

Held from this release: #1364 (correct in Python but it moves a
cross-port contract Decisions.java reproduces deliberately and
structured.ts shares), #1324 (real table regression), and seven
dependency and action bumps for a pass of their own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged, in 0.4.16.

The ordering argument is what makes this correct. _tournament_plan reads the criteria labels before router.predict, so Agent._check_question has not run yet and the refusal that would have named the question does not exist on this path. The TypeError then came from _criteria_items de-duplicating through a set, which is an implementation detail of the dedup rather than anything about the request, and it fell through the endpoint's catch-all as a 500 "inference failed" for input that was simply malformed. A 500 tells the caller to retry, and retrying an unhashable label will never work.

Converting it to the same ValueError the sibling duplicate and empty-label paths already raise is the right shape: one refusal class for one kind of fault, and 422 comes out of the existing mapping with nothing new added.

assert fake.calls == [] in the test is the assertion I would have asked for. Without it the test proves the status code and not that the request was refused while planning, and the whole point is that nothing is predicted.

The positive control in test_tournament_plan_names_the_question_on_unhashable_labels is appreciated too. A guard like this is one typo away from refusing every list-shaped criteria, and the control is what catches that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants